package com.airS.task.fileMgr.service.gk.impl;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.example.gk.PermissionExample;
import com.airS.task.fileMgr.mapper.gk.PermissionMapper;
import com.airS.task.fileMgr.model.gk.Permission;
import com.airS.task.fileMgr.query.gk.PermissionQuery;
import com.airS.task.fileMgr.service.gk.PermissionService;
import com.airS.task.fileMgr.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by alan on 2017/3/14.
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Permission queryPermissionById(Integer id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Permission> queryPermissions(PermissionQuery query) {
        PageUtil.startPage(query);
        List<Permission> permissions = permissionMapper.selectByExample(assemblyPermissionExample(query));
        Map<Integer, Permission> permissionMap = getAllPermissionToMap();
        for(Permission permission : permissions){
            Permission parentPerm = permissionMap.get(permission.getParentId());
            permission.setParentName(permission.getParentId() == 0 ? "根权限" : parentPerm == null ? "未定义" : parentPerm.getName());
        }
        return new Page<Permission>(permissions);
    }

    @Override
    public Map<Integer, Permission> getAllPermissionToMap(){
        Map<Integer, Permission> permissionMap = new HashMap<Integer, Permission>();
        for(Permission perm : getAllPermissionToList()){
            permissionMap.put(perm.getPermissionId(), perm);
        }
        return permissionMap;
    }

    @Override
    public List<Permission> getAllPermissionToList(){
        return permissionMapper.selectByExample(new PermissionExample());
    }

    @Override
    public Integer addPermission(Permission permission) {
        if(permission.getParentId() == 0){
            permission.setParentIds("0");
        }else{
            Permission parentPerm = getAllPermissionToMap().get(permission.getParentId());
            permission.setParentIds(parentPerm.getParentIds() + "," + parentPerm.getPermissionId());
        }
        permissionMapper.insert(permission);
        return permission.getPermissionId();
    }

    @Override
    public Integer updatePermissionById(Permission permission) {
        if(permission.getParentId() == 0){
            permission.setParentIds("0");
        }else{
            Permission parentPerm = getAllPermissionToMap().get(permission.getParentId());
            permission.setParentIds(parentPerm.getParentIds() + "," + parentPerm.getPermissionId());
        }
        return permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public Integer delPermissionById(Integer id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer delPermissionByIds(List<Integer> ids) {
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andPermissionIdIn(ids);
        return permissionMapper.deleteByExample(example);
    }

    private PermissionExample assemblyPermissionExample(PermissionQuery query){
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        if(StringUtils.hasText(query.getName())){
            criteria.andNameLike("%" + query.getName() + "%");
        }
        if(query.getParentId() != null){
            criteria.andParentIdEqualTo(query.getParentId());
        }
        if(StringUtils.hasText(query.getIsFunction())){
            criteria.andIsFunctionEqualTo(query.getIsFunction());
        }
        example.setOrderByClause("create_time desc");
        return example;
    }
}
